<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring静态属性赋值完全指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            position: relative;
            overflow: hidden;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        .code-dots {
            position: absolute;
            top: 14px;
            left: 20px;
            display: flex;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        pre {
            margin: 0;
            padding: 60px 30px 30px;
            overflow-x: auto;
            color: #d4d4d4;
            font-size: 14px;
            line-height: 1.6;
        }
        .method-card {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        .method-card:hover {
            transform: translateY(-5px);
            border-color: #667eea;
            box-shadow: 0 20px 40px rgba(102, 126, 234, 0.1);
        }
        .number-badge {
            background: linear-gradient(135deg, #667eea, #764ba2);
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            color: white;
            font-weight: bold;
            font-size: 18px;
        }
        .highlight-text {
            background: linear-gradient(to right, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="gradient-bg text-white py-20 px-6">
        <div class="max-w-6xl mx-auto animate-fade-in">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                    Spring静态属性赋值
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    掌握Spring框架中静态属性赋值的多种优雅实现方式
                </p>
                <div class="mt-8 flex justify-center gap-4">
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-code mr-2"></i>4种实现方式
                    </span>
                    <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        <i class="fas fa-clock mr-2"></i>5分钟阅读
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700 indent-8">
                    在Spring中给一个静态属性赋值可以通过多种方式实现。静态属性是类级别的属性，对于每个类来说只有一个副本，而不是每个对象都有一个副本。下面介绍几种常见的方法来给静态属性赋值：
                </p>
            </div>
        </div>
    </section>

    <!-- Concept Visualization -->
    <section class="py-8 px-6">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12 text-gray-800">
                <span class="highlight-text">实现方式概览</span>
            </h2>
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <div class="mermaid">
                    graph TD
                        A[Spring静态属性赋值] --> B[@Value注解]
                        A --> C[@ConfigurationProperties]
                        A --> D[静态块初始化]
                        A --> E[非静态方法赋值]
                        
                        B --> B1[从配置文件注入]
                        B --> B2[支持SpEL表达式]
                        
                        C --> C1[批量属性绑定]
                        C --> C2[类型安全]
                        
                        D --> D1[类加载时初始化]
                        D --> D2[无需Spring容器]
                        
                        E --> E1[通过setter方法]
                        E --> E2[依赖注入]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style C fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style D fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                        style E fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                </div>
            </div>
        </div>
    </section>

    <!-- Methods Section -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12 text-gray-800">
                <i class="fas fa-tools mr-3 text-purple-600"></i>
                实现方式详解
            </h2>

            <!-- Method 1 -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="number-badge">1</div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">使用@Value注解</h3>
                        <p class="text-gray-600 mb-6">
                            通过@Value注解可以将属性值从配置文件中注入到静态属性中。
                        </p>
                        <div class="code-block">
                            <div class="code-dots">
                                <div class="code-dot dot-red"></div>
                                <div class="code-dot dot-yellow"></div>
                                <div class="code-dot dot-green"></div>
                            </div>
                            <pre><code>import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyStaticClass {

    // 静态属性
    private static String staticProperty;

    @Value("${my.static.property}") // 从配置文件中获取值
    public void setStaticProperty(String value) {
        staticProperty = value;
    }

    public static String getStaticProperty() {
        return staticProperty;
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 2 -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="number-badge">2</div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">使用@ConfigurationProperties注解</h3>
                        <p class="text-gray-600 mb-6">
                            @ConfigurationProperties注解用于将配置文件中的属性值绑定到Bean的属性上。在这种方式下，需要创建一个配置类，并使用@ConfigurationProperties注解来绑定属性。
                        </p>
                        <div class="code-block mb-6">
                            <div class="code-dots">
                                <div class="code-dot dot-red"></div>
                                <div class="code-dot dot-yellow"></div>
                                <div class="code-dot dot-green"></div>
                            </div>
                            <pre><code>import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "my")
public class MyProperties {

    private static String staticProperty;

    public static String getStaticProperty() {
        return staticProperty;
    }

    public static void setStaticProperty(String value) {
        staticProperty = value;
    }
}</code></pre>
                        </div>
                        <p class="text-gray-600 mb-4 font-semibold">配置文件：</p>
                        <div class="code-block">
                            <div class="code-dots">
                                <div class="code-dot dot-red"></div>
                                <div class="code-dot dot-yellow"></div>
                                <div class="code-dot dot-green"></div>
                            </div>
                            <pre><code>my.staticProperty=value_from_config</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 3 -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="number-badge">3</div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">使用静态块初始化</h3>
                        <p class="text-gray-600 mb-6">
                            直接在类的静态块中进行赋值。
                        </p>
                        <div class="code-block">
                            <div class="code-dots">
                                <div class="code-dot dot-red"></div>
                                <div class="code-dot dot-yellow"></div>
                                <div class="code-dot dot-green"></div>
                            </div>
                            <pre><code>public class MyStaticClass {

    // 静态属性
    private static String staticProperty;

    static {
        staticProperty = "value_from_static_block";
    }

    public static String getStaticProperty() {
        return staticProperty;
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 4 -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8">
                <div class="flex items-start gap-6">
                    <div class="number-badge">4</div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">使用非静态方法给静态属性赋值</h3>
                        <div class="code-block">
                            <div class="code-dots">
                                <div class="code-dot dot-red"></div>
                                <div class="code-dot dot-yellow"></div>
                                <div class="code-dot dot-green"></div>
                            </div>
                            <pre><code>@SpringBootTest
public class SpringScopeTest {

    private static UserServiceImpl userService1;

    @Autowired
    public void setUserService1(UserServiceImpl userService) {
        userService1 = userService;
    }

    @Test
    public void test1() {
        userService1.add();
    }

}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Best Practices -->
    <section class="py-16 px-6 bg-gray-100">
        <div class="max-w-4xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12 text-gray-800">
                <i class="fas fa-lightbulb mr-3 text-yellow-500"></i>
                最佳实践建议
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl p-6 shadow-md hover:shadow-xl transition-shadow">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-check-circle text-green-500 text-2xl mr-3"></i>
                        <h3 class="text-xl font-semibold">选择合适的方式</h3>
                    </div>
                    <p class="text-gray-600">根据具体场景选择最适合的实现方式，配置文件